﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace EmailCenter_SEK53
{
    public class EmailAccount
    {
        public string Host = "pop.gmail.com";
        public int Port = 995;
        public string Email = "ncc1990@gmail.com";
        public string Password = "pass";
        public bool IsSecure = false;
        public int Id = -1;

        Pop3Client client = null;

        int nNewEmails = 0;
        int iCurrentEmailID = 0;

        public int Status { get; protected set; }

        //public static int UNINITIALIZED = 0;
        public static int NOTCONNECTED = 0;
        public static int CONNECTED = 1;

        public EmailAccount()
        {
            Status = NOTCONNECTED;
        }

        public EmailAccount(string username, string password)
        {
            Status = NOTCONNECTED;
            this.Email = username;
            this.Password = password;
        }

        public EmailAccount(string username, string password, bool isSecure)
        {
            Status = NOTCONNECTED;
            this.Email = username;
            this.Password = password;
            this.IsSecure = isSecure;
        }

        public void Connect()
        {
            if (client == null)
                client = new Pop3Client(Host, Port, Email, Password, true);

            client.Connect();
            Status = CONNECTED;
        }

        public void Close()
        {
            if (client != null)
            {
                client.Close();
                
                client = null;
                Status = NOTCONNECTED;
            }
        }

        public int GetNewEmailsCount()
        {
            nNewEmails = client.GetEmailCount();
            return nNewEmails;
        }

        public Email GetEmail(int emailid)
        {
            iCurrentEmailID = emailid;
            return client.FetchEmail(emailid);
        }

        public Email GetCurrentEmail()
        {
            return client.FetchEmail(iCurrentEmailID);
        }

        public Email GetPreviousEmail()
        {
            Email email = client.FetchEmail(iCurrentEmailID - 1);
            if (email != null)
                iCurrentEmailID--;

            return email;
        }

        public Email GetNextEmail()
        {
            Email email = client.FetchEmail(iCurrentEmailID + 1);
            if (email != null)
                iCurrentEmailID++;
            else
                nNewEmails = client.GetEmailCount();

            return email;
        }

        public EmailParser DownloadAndParseCurrentEmail(string downloadTo)
        {
            string path = "Email";
            string emailText = client.GetEmailText(iCurrentEmailID);

            EmailParser email = new EmailParser(emailText);
            Directory.CreateDirectory(path);

            DateTime now = DateTime.Now;
            string name = now.Year.ToString("D4") + now.Month.ToString("D2") +
                        now.Day.ToString("D2") + now.Hour.ToString("D2") +
                        now.Minute.ToString("D2") + now.Second.ToString("D2") +
                        now.Millisecond.ToString("D3");

            path += "\\" + name + ".txt";
            StreamWriter myfile = File.CreateText(path);
            myfile.Write(emailText);
            myfile.Close();
            return email;
        }

        public List<EmailParser> DownloadAndParseAll(string downloadTo)
        {
            List<EmailParser> list = new List<EmailParser>();
            int n = this.GetNewEmailsCount();

            for (iCurrentEmailID = 1; iCurrentEmailID <= n; iCurrentEmailID++)
            {
                list.Add(DownloadAndParseCurrentEmail(downloadTo));
            }

            iCurrentEmailID = 0;
            return list;
        }
    }
}
